package org.syncsoft.overalt.gui.impl.componentPlugin;

import java.util.HashMap;
import java.util.Map;

import org.syncsoft.overalt.gui.api.WindowContextValue;
import org.syncsoft.overalt.gui.api.JValuedCheckBox;
import org.syncsoft.overalt.gui.api.componentPlugin.IComponentPlugin;

/**
 * 
 * @author tptfc
 *
 */
public class CheckBoxComponentPlugin implements IComponentPlugin {
	private Map<String, JValuedCheckBox> checkBoxs;
	
	{
		setCheckBoxs(new HashMap<String, JValuedCheckBox>());
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.IComponentPlugin#buildContext(org.syncsoft.overalt.gui.swing.api.ContextWindowValue)
	 */
	@Override
	public void buildContext(WindowContextValue context) {
		for (String key : getCheckBoxs().keySet()) {
			JValuedCheckBox check = getCheckBox(key);
			
			if (check != null && check.isSelected()) {
				Object value = check.getValue();
				context.addMultiValue(key, value);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.IComponentPlugin#getContext()
	 */
	@Override
	public WindowContextValue getContext() {
		WindowContextValue context = new WindowContextValue();
		buildContext(context);
		return context;
	}
	
	/**
	 * @precondition key and name not is null
	 * @param key
	 * @param name
	 * @return
	 */
	public JValuedCheckBox newCheckBox(String key, String name) {
		JValuedCheckBox check = null;
		
		if (key != null && name != null) {
			check = new JValuedCheckBox(name);
			getCheckBoxs().put(key, check);
		}

		return check;
	}
	
	/**
	 * @precondition key and name not is null
	 * @param key
	 * @param name
	 * @return
	 */
	public JValuedCheckBox newCheckBox(String key, String name, Object value) {
		JValuedCheckBox check = null;
		
		if (key != null && name != null) {
			check = new JValuedCheckBox(name, value);
			getCheckBoxs().put(key, check);
		}

		return check;
	}
	
	/**
	 * 
	 * @param key
	 * @return
	 */
	public JValuedCheckBox getCheckBox(String key) {
		return getCheckBoxs().get(key);
	}

	private Map<String, JValuedCheckBox> getCheckBoxs() {
		return checkBoxs;
	}

	private void setCheckBoxs(Map<String, JValuedCheckBox> checkBoxs) {
		this.checkBoxs = checkBoxs;
	}
}
